package com.capgemini.remind.controller;

import com.capgemini.commons.VelocityUtils;
import com.capgemini.core.AjaxResponse;
import com.capgemini.core.PageList;
import com.capgemini.interceptor.DealerNumUtils;
import com.capgemini.remind.pojo.*;
import com.capgemini.remind.service.CommonsService;
import com.capgemini.remind.service.CustomerGroupService;
import com.capgemini.wechat.service.VehicleService;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerGroup")
public class CustomerGroupController {
	// 日志记录器
	public final Logger log = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private CustomerGroupService customerGroupService;
	@Autowired
	private CommonsService commonsService;
	@Autowired
	private VehicleService vehicleService;

	@RequestMapping("/manage/{dealerNum}")
	public Object manage(@PathVariable("dealerNum") String dealerNum1
			, @RequestParam(required=false, name="returnBtnEnabled") String returnBtnEnabled
			, @RequestParam(name = "send", required = false) String sendFlag
			, @RequestParam(name = "remindMonth", required = false) String remindMonth
			) {
		String dealerNum = DealerNumUtils.getDealerNum();

		ModelAndView mv = new ModelAndView("/remind/customerGroup.jsp");

		mv.addObject("dealerNum", dealerNum);
		
		// for return back
		mv.addObject("returnBtnEnabled",returnBtnEnabled);
		mv.addObject("send", sendFlag);
		mv.addObject("remindMonth",remindMonth);

		//车型
		List<CodeValue> vehicleBrands = commonsService.queryByCodeValueByType("vehicleBrand");
		vehicleService.prepareVehicleBrandForDealer(dealerNum, vehicleBrands);
		mv.addObject("vehicleBrands", vehicleBrands);

		return mv;
	}

	@RequestMapping("/list")
	@ResponseBody
	public Object list(PageList<CustomerGroup> pageList, CustomerGroup pojo) {
		AjaxResponse ajaxResponse = new AjaxResponse();

		pageList.setParam(pojo);
		customerGroupService.queryPage(pageList);

		ajaxResponse.setData(pageList);

		return ajaxResponse;
	}

	@RequestMapping("/load/{id}")
	@ResponseBody
	public Object loadById(@PathVariable("id") String id) {
		AjaxResponse ajaxResponse = new AjaxResponse();

		ajaxResponse.setData(customerGroupService.getById(id));

		return ajaxResponse;
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public Object save(CustomerGroup pojo) {
		AjaxResponse ajaxResponse = new AjaxResponse();

		if(pojo.getId() == null){
			customerGroupService.insert(pojo);
			ajaxResponse.setData(pojo);
		}else{
			customerGroupService.updateById(pojo);
		}

		return ajaxResponse;
	}

	@RequestMapping("/del/{id}")
	@ResponseBody
	public Object deleteById(@PathVariable("id") String id) {
		AjaxResponse ajaxResponse = new AjaxResponse();


		if(StringUtils.isBlank(id)){
			ajaxResponse.setSuccess(false);
			ajaxResponse.setErrorCode("PARAM-LOSS");
			ajaxResponse.setMessage("参数丢失");
		}else{
			customerGroupService.deletePhysicalById(id);
		}

		return ajaxResponse;
	}



	@RequestMapping("/customer/list")
	@ResponseBody
	public Object customerPageList(PageList<AnnouncementCustomerInfo> pageList, AnnouncementCustomerInfo pojo) {
		AjaxResponse ajaxResponse = new AjaxResponse();

		pageList.setParam(pojo);
		customerGroupService.queryCustomerPage(pageList);

		ajaxResponse.setData(pageList);

		return ajaxResponse;
	}


	@RequestMapping("/customer/addList")
	@ResponseBody
	public Object queryCustomerPageForAdd(PageList<AnnouncementCustomerInfo> pageList, AnnouncementCustomerInfo pojo) {
		AjaxResponse ajaxResponse = new AjaxResponse();

		pageList.setParam(pojo);
		customerGroupService.queryCustomerPageForAdd(pageList);

		ajaxResponse.setData(pageList);

		return ajaxResponse;
	}


	@RequestMapping("/customer/del")
	@ResponseBody
	public Object delCustomerFromGroup(@RequestParam("dealerNum")String dealerNum
			,@RequestParam("customerId")String customerId
			,@RequestParam("groupId")String groupId
	) {
		AjaxResponse ajaxResponse = new AjaxResponse();

		customerGroupService.delCustomerFromGroup(dealerNum, customerId, groupId);

		return ajaxResponse;
	}



	@RequestMapping("/customer/addAll")
	@ResponseBody
	public Object sendAll(@RequestParam("dealerNum")String dealerNum, AnnouncementCustomerInfo remind
			, @RequestParam("groupId") String groupId) {
		AjaxResponse ajaxResponse = new AjaxResponse();

		PageList<AnnouncementCustomerInfo> pageList = new PageList<AnnouncementCustomerInfo>();
		pageList.setParam(remind);
		pageList.setLimit(Integer.MAX_VALUE);
		customerGroupService.queryCustomerPageForAdd(pageList);
		List<String> customerIdList = new ArrayList<String>();
		for (AnnouncementCustomerInfo announcementCustomerInfo : pageList.getItems()) {
			customerIdList.add(announcementCustomerInfo.getCustomerId());
		}

		customerGroupService.addCustomerToGroup(dealerNum, groupId, customerIdList);

		return ajaxResponse;
	}



	@RequestMapping("/customer/addSelected")
	@ResponseBody
	public Object sendSelected(@RequestParam("dealerNum")String dealerNum, @RequestParam("groupId") String groupId
			, @RequestParam("customerId") List<String> selectedIdList) {
		AjaxResponse ajaxResponse = new AjaxResponse();

		if(selectedIdList.isEmpty()){
			ajaxResponse.setSuccess(false);
			ajaxResponse.setErrorCode("ERROR");
			ajaxResponse.setMessage("请先选择要添加的客户.");

			return ajaxResponse;
		}

		customerGroupService.addCustomerToGroup(dealerNum, groupId, selectedIdList);


		return ajaxResponse;
	}



}
